package JUC.CompletableFuture;

import java.util.concurrent.*;

/**
 * @author CKC
 * @CreateTime 2022/6/7 13:44
 */
public class CompletableFutureBuildDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(3);

        try {
            CompletableFuture<Integer> exceptionally = CompletableFuture.supplyAsync(() -> {
                System.out.println(Thread.currentThread().getName() + "---come in");
                int i = ThreadLocalRandom.current().nextInt(10);
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (Throwable e) {
                    e.printStackTrace();
                }
                System.out.println("----1秒钟后出结果：" + i);
                if (i > 7) {
                    int f = 10 / 0;
                }
                return i;
            }, executorService).whenComplete((v, e) -> {
                if (e == null) {
                    System.out.println("-----计算完成，更新系统UpdateValue: " + v);
                }
            }).exceptionally(e -> {
                e.printStackTrace();
                return null;
            });
            System.out.println(Thread.currentThread().getName() + "  忙其他事情去了");
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            executorService.shutdown();
        }


    }

    //使用get容易造成阻塞
    private static void future1() throws InterruptedException, ExecutionException {
        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "---come in");
            int i = ThreadLocalRandom.current().nextInt(10);
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (Throwable e) {
                e.printStackTrace();
            }
            System.out.println("----1秒钟后出结果：" + i);
            return i;
        });
        System.out.println(Thread.currentThread().getName() + "  忙其他事情去了");
        System.out.println(integerCompletableFuture.get());
    }

    private static void demo1() throws InterruptedException, ExecutionException {
        //CompletableFuture completableFuture = new CompletableFuture();  不建议是用默认的构造方法
        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
            System.out.println(Thread.currentThread().getName()); //ForkJoinPool.commonPool-worker-1
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        System.out.println(completableFuture.get()); //null

        ExecutorService executorService = Executors.newFixedThreadPool(3);

        CompletableFuture.runAsync(() -> {
            System.out.println(Thread.currentThread().getName()); //pool-1-thread-1
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }, executorService);


        CompletableFuture<String> completableFuture1 = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName());
            return "supplyAsync";
        }, executorService);
        System.out.println(completableFuture1.get());


        executorService.shutdown();
    }
}
